#include "stdio.h"
#include "stdlib.h"
#include "sys/time.h"
#include "time.h"

int main(int argc, char *argv[])
{
	int m, n, k, i;
	double beta = 2;
	struct timeval start, finish;
	double duration;
	m = n = k = atoi(argv[1]);
	int sizeofa = m * k;
	int sizeofb = k * n;
	int sizeofc = m * n;
	double *A = (double *)malloc(sizeof(double) * sizeofa);
	double *B = (double *)malloc(sizeof(double) * sizeofb);
	double *C = (double *)malloc(sizeof(double) * sizeofc);
	for (i = 0; i < sizeofa; i++)
	{
		A[i] = i % 3 + 1; // (rand() % 100) / 100.0;
	}

	for (i = 0; i < sizeofb; i++)
	{
		B[i] = i % 3 + 1; //(rand()%100)/10.0;
	}

	for (i = 0; i < sizeofc; i++)
	{
		C[i] = 0.1;
	}
	// C=A*B + beta*C
	srand((unsigned)time(NULL));
	gettimeofday(&start, NULL);
	for (int i = 0; i < m; i++)
	{ // C[i]
		for (int j = 0; j < n; j++)
		{ // C[i][j]
			C[i * m + j] = beta * C[i * m + j];
			for (int p = 0; p < k; p++)
			{
				C[i * m + j] += A[i * k + p] * B[p * n + j];
			}
		}
	}
	gettimeofday(&finish, NULL);

	// ת�ɳ�����
	duration = (double)(finish.tv_sec - start.tv_sec) + (double)(finish.tv_usec - start.tv_usec) / 1.0e6;
	double gflops = 4.0 * m * n * k;
	gflops = gflops / duration * 1.0e-9;

	FILE *fp;
	fp = fopen("timeDGEMM.txt", "a"); // ׷��д
	fprintf(fp, "%dx%dx%d\t%lf s\t%lf GFLOPS\n", m, n, k, duration, gflops);
	fclose(fp);

	free(A);
	free(B);
	free(C);
	return 0;
}
